﻿using System;
using System.Data;
using System.Linq;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        private void YGRAB50AR()
        {
            string value = "";
            DataRow dwMaster = null;
            DataRow dwABK = null;
            
            DataRow dwAR = null;
            int rStart = 0;
            int nRow = 1;
            int nPRow = 1;
            int idx = 0;
            int pages = 0;
            dwMaster = dsSource.Tables["ABPOS"].Rows[0];
              dwABK = dsSource.Tables["ABKOPF"].Rows[0];
         
            if (dsSource.Tables["ARANS"].Rows.Count > 0)
              {
                  dwAR = dsSource.Tables["ARANS"].Rows[0];
              } 
            string art = dwMaster["ABPART"].ToString();
            string var = dwMaster["ABPVAR"].ToString();
            string color = dwMaster["ABPFARBE"].ToString();
            #region====Header======
            exAgent.SetCellValue(3, 29, date);
            exAgent.SetCellValue(1, 23, "*" + dwMaster["ABPAUFKRS"].ToString() + dwMaster["ABPAUFLFD"].ToString() + dwMaster["ABPAUFPOS"].ToString() + "*");//barcode

            exAgent.SetCellValue(3, 5, dwMaster["ABPAUFKRS"].ToString() + "-" + Helpers.Helper.GetSubRollNum(dwMaster["ABPAUFLFD"].ToString())
                + "-" + Helpers.Helper.GetSubRollNum(dwMaster["ABPAUFPOS"].ToString()));
            exAgent.SetCellValue(3, 15, dwABK["ABKKDAUFTNR"].ToString());
            //exAgent.SetCellValue(3, 23, dwMaster["ABPFARBE"].ToString());
            exAgent.SetCellValue(4, 5, art);
            var qGS = from DataRow row in dsSource.Tables["GSDAT"].AsEnumerable()
                      where row["GSART"].ToString() == art
                      && row["GSVAR"].ToString() ==var
                      select row;
            foreach (DataRow row in qGS)
            {
                exAgent.SetCellValue(4, 5, art.Trim()+":"+GetGB2312Value(row["GSHANDLNR"].ToString().Trim()));
            }
            exAgent.SetCellValue(4, 21, color);
            var qGC = from DataRow row in dsSource.Tables["GSCOLOR"].AsEnumerable()
                      where row["GSCART"].ToString() == art
                      && row["GSCVAR"].ToString() == var
                      && row["GSCFARBE"].ToString() ==color
                      select row;
            foreach (DataRow row in qGC)
            {
                exAgent.SetCellValue(4, 21, color.Trim() + ":" + GetGB2312Value(row["GSCBEZ"].ToString().Trim()));
            }
            var qM = from DataRow row in dsSource.Tables["GSMATK"].AsEnumerable()
                  where row["GSMART"].ToString() == art
                  && row["GSMVAR"].ToString() == var
                  select row;
            string composition = "";
            foreach (DataRow row in qM)
            {
                composition += row["GSMRBEZ"].ToString().Trim() + Convert.ToInt16(row["GSMRANT"].ToString().Trim()) / 10 + "%";
            }
            exAgent.SetCellValue(5, 15, composition);
            exAgent.SetCellValue(5, 5, Convert.ToDouble(dwMaster["ABPSOLLMENGE5"].ToString()) / 100);
            exAgent.SetCellValue(5, 29, dwMaster["ABPLIEFDAT"].ToString());
            exAgent.SetCellValue(8, 5, dwMaster["ABPFILLER1"].ToString());
            if (dwAR != null)
            {
                exAgent.SetCellValue(8, 31, Helpers.Helper.GetSubRollNum(dwAR["ARAREZNR"].ToString()));                
                exAgent.SetCellValue(9, 31, Convert.ToDouble(dwAR["ARAMGKG"].ToString()) / 1000);
                exAgent.SetCellValue(10, 31, Convert.ToDouble(dwAR["ARAFLOTTE"].ToString()) / 1000);
                            exAgent.SetCellValue(4, 29, dwAR["ARAMNRKRS"].ToString() + "-" + Helpers.Helper.GetSubRollNum(dwAR["ARAMLFDNR"].ToString())
                + "-" + Helpers.Helper.GetSubRollNum(dwAR["ARAMPOS"].ToString()));  
            }                

            #endregion

            #region====Material=========
            rStart = 16;
            nRow = 2;
            nPRow = 2;
            idx = 0;
            //LRPTYP MSBEZ        
            var qD = from DataRow row in dsSource.Tables["LRPOS"].AsEnumerable()
                     where row["LRPTYP"].ToString() == "F"
                     select row;
            int ycount = qD.Count();
            foreach (DataRow row in qD)
            {
                exAgent.SetCellValue(rStart + idx * nRow, 1, "*" + GetGB2312Value(row["LRPKOMP"].ToString()).Trim()+"*");
                exAgent.SetCellValue(rStart + idx * nRow, 11,GetGB2312Value(row["MSBEZ"].ToString().Trim()));
                exAgent.SetCellValue(rStart + idx * nRow, 21, "*"+row["LRPDRUCK"].ToString().Trim()+"*");
                idx++;
                if (idx < ycount)
                {
                    exAgent.RangeCopy(16, 17, rStart + idx * nRow, rStart + idx * nRow + 1);
                }
            }
            #endregion
            #region======Step=========
            rStart = 106;
            nRow = 2;
            nPRow =1;
            idx = 0;
           var qC = from DataRow row in dsSource.Tables["LRPOS"].AsEnumerable()
                     where row["LRPTYP"].ToString() == "C"
                     select row;
              ycount = qC.Count();
              foreach (DataRow row in qC) 
              {
                  exAgent.SetCellValue(rStart + idx * nRow, 1, "*" + GetGB2312Value(row["LRPKOMP"].ToString()).Trim() + "*");
                exAgent.SetCellValue(rStart + idx * nRow, 11,GetGB2312Value(row["MSBEZ"].ToString().Trim()));
                exAgent.SetCellValue(rStart + idx * nRow, 21, "*" + row["LRPDRUCK"].ToString().Trim() + "*");
                idx++;
                if (idx < ycount)
                {
                    exAgent.RangeCopy(106, 107, rStart + idx * nRow, rStart + idx * nRow + 1);
                } 
            } 
            #endregion     
            #region=========footer===========
            exAgent.SetCellValue(rStart + idx  + 3, 2, "制表人：			_____________");
            exAgent.SetCellValue(rStart + idx + 3 + 1, 2, "日期：			_____________");
            exAgent.SetCellValue(rStart + idx + 3, 10, "主管：			_____________");
            exAgent.SetCellValue(rStart + idx + 3 + 1, 10, "日期：			_____________");
            exAgent.SetCellValue(rStart + idx + 3, 23, "经理：			_____________");
            exAgent.SetCellValue(rStart + idx + 3 + 1, 23, "日期：			_____________");
            #endregion
        } 
    }
}